package com.llmcu.graphic.designpatterns.interpreter;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Setter
@Getter
public class CommandListNode extends Node {
    private List<CommandNode> commandNodeList;
    public static final String END_TOKEN = "end";

    @Override
    public void parse(Context context) {
        commandNodeList = new ArrayList<>();

        while (context.getCurrentToken() != null && context.getCurrentToken().length() > 0
                && !Objects.equals(context.getCurrentToken(), END_TOKEN)) {
            if (Objects.equals(context.getCurrentToken(), RepeatCommandNode.REPEAT_TOKEN)) {
                RepeatCommandNode repeatCommandNode = new RepeatCommandNode();
                commandNodeList.add(repeatCommandNode);
                repeatCommandNode.parse(context);
            } else {
                PrimitiveCommandNode primitiveCommandNode = new PrimitiveCommandNode();
                commandNodeList.add(primitiveCommandNode);
                primitiveCommandNode.parse(context);
            }
        }
        context.skip(END_TOKEN);
    }

    @Override
    public String toString() {
        return commandNodeList.toString();
    }
}
